<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="650px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="125px"
    >
      <el-form-item label="人员名称" prop="ryCode">
        <el-select
          :clearable="true"
          v-model="dataForm.ryCode"
          placeholder="请选择"
          class="xiaLa"
          style="width: 90%"
        >
          <el-option
            v-for="(item, index) in ryOptions"
            :key="index"
            :label="item.ryName"
            :value="item.ryCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="奖金项名称" prop="jjxId">
        <el-select
          :clearable="true"
          v-model="dataForm.jjxId"
          placeholder="请选择"
          class="xiaLa"
          style="width: 90%"
        >
          <el-option
            v-for="(item, index) in jjxOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="奖金金额" prop="jjJe">
        <el-input-number
          v-model="dataForm.jjJe"
          :precision="2"
          :step="1"
          :min="0"
          style="width: 200px"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="奖金说明" prop="jjSm">
        <el-input
          v-model="dataForm.jjSm"
          placeholder="奖金说明"
          :rows="3"
          type="textarea"
          style="width: 90%"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, updateById } from "@/api/jiangjin/jiangjinfpjjfpgrdxb";
import { getRyfzmxbByJjDeptId } from "@/api/jiangjin/jiangjinryfzmxb";
import { getJjDeptJjx } from "@/api/jiangjin/jiangjinjjxxxb";

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      ryOptions: [],
      jjxOptions: [],
      deptId: "",
      dataForm: {
        id: 0,
        jjDeptId: "",
        fpId: "",
        bmjjFpId: "",
        deptId: "",
        deptCode: "",
        deptName: "",
        ryCode: "",
        ryName: "",
        jjxId: "",
        jjxMc: "",
        jjJe: "",
        jjSm: "",
        createTime: "",
        editTime: "",
        createDeptId: "",
        createDeptName: "",
        createUserId: "",
        createUserName: "",
        editDeptId: "",
        editDeptName: "",
        editUserId: "",
        editUserName: "",
        dataState: "",
        tenantId: "",
      },
      dataRule: {
        ryCode: [
          { required: true, message: "人员名称不能为空", trigger: "blur" },
        ],
        jjxId: [{ required: true, message: "奖金项不能为空", trigger: "blur" }],
        jjxMc: [{ required: true, message: "奖金项不能为空", trigger: "blur" }],
        // jjJe: [
        //   { required: true, message: '奖金金额不能为空', trigger: 'blur' }
        // ]
      },
    };
  },
  methods: {
    //默认值  设置
    initMRZ() {
      this.dataForm.bmjjFpId = 0;

      this.dataForm.jjDeptId = this.deptId;
      this.dataForm.fpId = 0;

      this.dataForm.deptId = 0;
      this.dataForm.deptCode = "";
      this.dataForm.deptName = "";
    },
    init(id, deptId) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.deptId = deptId;

      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.initMRZ();
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then((response) => {
            this.dataForm = response.data.data;
          });
        }
      });

      getRyfzmxbByJjDeptId(deptId).then((res) => {
        const xData = res.data.data;
        this.ryOptions = [];
        xData.map((item) => {
          this.ryOptions.push({
            ryCode: item.ryCode.toString(),
            ryName: item.ryName,
          });
        });
      });

      getJjDeptJjx(deptId).then((res) => {
        const xData = res.data.data;
        this.jjxOptions = [];
        xData.map((item) => {
          this.jjxOptions.push({
            value: item.id,
            label: item.jjxMc,
          });
        });
      });
    },
    getRyUserName(value) {
      let lResult = "";
      for (var i = 0; i < this.ryOptions.length; i++) {
        if (this.ryOptions[i].ryCode == value) {
          lResult = this.ryOptions[i].ryName;
          break;
        }
      }
      return lResult;
    },
    getJjxName(value) {
      let lResult = "";
      this.jjxOptions.find((item) => {
        if (item.value === value) {
          lResult = item.label;
        }
      });
      return lResult;
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          this.$set(
            this.dataForm,
            "ryName",
            this.getRyUserName(this.dataForm["ryCode"].toString())
          );
          this.$set(
            this.dataForm,
            "jjxMc",
            this.getJjxName(this.dataForm.jjxId)
          );

          if (this.dataForm.id) {
            updateById(this.dataForm)
              .then((data) => {
                this.$notify.success("修改成功");
                this.visible = false;
                this.$emit("refreshDataList");
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(this.dataForm)
              .then((data) => {
                this.$notify.success("添加成功");
                this.visible = false;
                this.$emit("refreshDataList");
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
  },
};
</script>
